extends Node2D

var camera:Node2D
@onready var heart: Node2D = $heart
@onready var focus: Node2D = $focus
@onready var items: Node2D = $items
@onready var layer_1: TileMapLayer = $tile_map/layer1
@onready var layer_0: TileMapLayer = $tile_map/layer0
@onready var shards: Node2D = $shards
@onready var finale: Node2D = $finale

var offset=Vector2i(1,0)
var is_building=false

func _enter(new_camera):
	self.visible=true
	camera=new_camera
	camera.ui.visible=true
	heart.add_child(camera)
	heart.moveable=true
	heart.bgm.playing=true

func _exit():
	self.visible=false
	heart.remove_child(camera)
	heart.moveable=false
	heart.bgm.playing=false
	camera.ui.visible=false
	return camera

func _build_in():
	is_building=true
	heart.moveable=false
	focus.visible=true
	focus.is_building=true

func _build_out():
	is_building=false
	heart.moveable=true
	focus.visible=false
	focus.is_building=false
	if focus.cur_state==focus.state.build:
		focus._state_shift()

func test01():
	var test=preload("res://node/diamond.tscn").instantiate()
	test._setting(Vector2i(3,1),layer_1)
	items.add_child(test)
	var test1=preload("res://node/triangle.tscn").instantiate()
	test1._setting(Vector2i(0,1),layer_1)
	items.add_child(test1)
	var test2=preload("res://node/cube.tscn").instantiate()
	test2._setting(Vector2i(1,0),layer_1)
	items.add_child(test2)
	var test3=preload("res://node/triangle.tscn").instantiate()
	test3._setting(Vector2i(4,-1),layer_1)
	items.add_child(test3)
	var shard1=preload("res://node/shard.tscn").instantiate()
	shard1._setting(Vector2i(4,-2),layer_1,{"line":"A1"})
	shards.add_child(shard1)
	var shard2=preload("res://node/shard.tscn").instantiate()
	shard2._setting(Vector2i(1,-4),layer_1,{"line":"A2"})
	shards.add_child(shard2)
	var shard3=preload("res://node/shard.tscn").instantiate()
	shard3._setting(Vector2i(0,1),layer_1,{"line":"A3"})
	shards.add_child(shard3)
	var shard4=preload("res://node/shard.tscn").instantiate()
	shard4._setting(Vector2i(6,-4),layer_1,{"line":"A4"})
	shards.add_child(shard4)

func _summon_shard(pos,link,cycle=6,attack=4):
	var shard=preload("res://node/shard.tscn").instantiate()
	shard.pos=pos-offset
	shard.link["line"]=link
	shard.cycle=cycle
	shard.attack=attack
	shards.add_child(shard)

func _summon_item(pos,type):
	var item
	match type:
		"cube":
			item=preload("res://node/cube.tscn").instantiate()
		"triangle":
			item=preload("res://node/triangle.tscn").instantiate()
		"diamond":
			item=preload("res://node/diamond.tscn").instantiate()
	item._setting(pos-offset,layer_1)
	items.add_child(item)

func _summon_fin(pos,level,target,light_house):
	var fin=preload("res://node/target.tscn").instantiate()
	fin.level=level
	fin.target=target
	fin.light_house=light_house
	fin._setting(pos-offset,layer_1)
	finale.add_child(fin)

func _level_1(reset=false):
	if reset:
		global.flag_list["level1_fin"]=0
		heart.pos=Vector2i(28,15)-offset
		layer_1.set_cell(Vector2i(19,13),3,Vector2i(0,0))
	_summon_shard(Vector2i(19,22),"A",8,4)
	_summon_item(Vector2i(28,20),"cube")
	_summon_item(Vector2i(18,19),"cube")
	_summon_item(Vector2i(20,19),"diamond")
	_summon_item(Vector2i(28,19),"diamond")
	_summon_item(Vector2i(28,25),"diamond")
	_summon_item(Vector2i(28,21),"triangle")
	_summon_item(Vector2i(28,18),"triangle")
	_summon_fin(Vector2i(19,15),["level1_fin","level1_pass"],1,Vector2i(19,13))

func _level_2(reset=false):
	if reset:
		global.flag_list["level2_fin"]=0
		heart.pos=Vector2i(3,-2)-offset
		layer_1.set_cell(Vector2i(3,-16),3,Vector2i(0,0))
	_summon_shard(Vector2i(3,-8),"B1")
	_summon_shard(Vector2i(-2,-14),"B2")
	_summon_shard(Vector2i(2,-12),"B3")
	_summon_shard(Vector2i(4,-14),"B4")
	_summon_item(Vector2i(2,-1),"diamond")
	_summon_item(Vector2i(3,-9),"triangle")
	_summon_item(Vector2i(4,-1),"triangle")
	_summon_item(Vector2i(4,-8),"cube")
	_summon_item(Vector2i(-3,-8),"cube")
	_summon_item(Vector2i(-4,-14),"diamond")
	_summon_fin(Vector2i(3,-14),["level2_fin","level2_pass"],4,Vector2i(3,-16))

func _level_3(reset=false):
	if reset:
		global.flag_list["level3_fin"]=0
		heart.pos=Vector2i(15,-26)-offset
		layer_1.set_cell(Vector2i(13,-33),3,Vector2i(0,0))
	_summon_shard(Vector2i(14,-37),"C")
	_summon_shard(Vector2i(15,-38),"C")
	_summon_shard(Vector2i(16,-37),"C")
	_summon_shard(Vector2i(13,-28),"C")
	_summon_shard(Vector2i(17,-28),"C")
	_summon_item(Vector2i(15,-28),"diamond")
	_summon_item(Vector2i(15,-30),"diamond")
	_summon_item(Vector2i(19,-30),"diamond")
	_summon_item(Vector2i(11,-30),"diamond")
	_summon_item(Vector2i(15,-32),"cube")
	_summon_item(Vector2i(15,-37),"cube")
	_summon_item(Vector2i(15,-25),"cube")
	_summon_item(Vector2i(16,-31),"cube")
	_summon_item(Vector2i(11,-32),"cube")
	_summon_item(Vector2i(14,-31),"triangle")
	_summon_item(Vector2i(19,-32),"triangle")
	_summon_fin(Vector2i(15,-31),["level3_fin","level3_pass"],5,Vector2i(13,-33))

func _level_4(reset=false):
	if reset:
		global.flag_list["level4_fin"]=0
		heart.pos=Vector2i(-8,4)-offset
		layer_1.set_cell(Vector2i(-24,8),3,Vector2i(0,0))
	_summon_shard(Vector2i(-14,-1),"D")
	_summon_shard(Vector2i(-18,3),"D")
	_summon_shard(Vector2i(-12,9),"D")
	_summon_shard(Vector2i(-16,13),"D")
	_summon_shard(Vector2i(-18,11),"D")
	_summon_item(Vector2i(-10,3),"diamond")
	_summon_item(Vector2i(-16,5),"diamond")
	_summon_item(Vector2i(-14,7),"cube")
	_summon_item(Vector2i(-10,5),"cube")
	_summon_item(Vector2i(-8,3),"triangle")
	_summon_item(Vector2i(-8,5),"triangle")
	_summon_item(Vector2i(-18,3),"triangle")
	_summon_item(Vector2i(-12,9),"triangle")
	_summon_fin(Vector2i(-22,8),["level4_fin","level4_pass"],5,Vector2i(-24,8))

func _level_5(reset=false):
	if reset:
		global.flag_list["level5_fin"]=0
		heart.pos=Vector2i(37,-21)-offset
		layer_1.set_cell(Vector2i(27,-31),3,Vector2i(0,0))
	_summon_shard(Vector2i(31,-27),"E")
	_summon_shard(Vector2i(31,-25),"E")
	_summon_shard(Vector2i(33,-27),"E")
	_summon_shard(Vector2i(31,-23),"E")
	_summon_shard(Vector2i(35,-27),"E")
	_summon_item(Vector2i(34,-21),"diamond")
	_summon_item(Vector2i(36,-21),"diamond")
	_summon_item(Vector2i(38,-21),"diamond")
	_summon_item(Vector2i(37,-25),"diamond")
	_summon_item(Vector2i(37,-20),"cube")
	_summon_item(Vector2i(33,-21),"cube")
	_summon_item(Vector2i(37,-22),"cube")
	_summon_item(Vector2i(37,-24),"cube")
	_summon_item(Vector2i(36,-20),"triangle")
	_summon_item(Vector2i(38,-22),"triangle")
	_summon_item(Vector2i(38,-20),"triangle")
	_summon_fin(Vector2i(29,-29),["level5_fin","level5_pass"],5,Vector2i(27,-31))

func _level_6(reset=false):
	if reset:
		global.flag_list["level6_fin"]=0
		heart.pos=Vector2i(65,-26)-offset
		layer_1.set_cell(Vector2i(48,-27),3,Vector2i(0,0))
	_summon_shard(Vector2i(54,-27),"F1")
	_summon_shard(Vector2i(59,-24),"F2")
	_summon_shard(Vector2i(57,-27),"F3")
	_summon_shard(Vector2i(52,-23),"F4")
	_summon_item(Vector2i(59,-30),"diamond")
	_summon_item(Vector2i(54,-21),"diamond")
	_summon_item(Vector2i(53,-30),"diamond")
	_summon_item(Vector2i(65,-23),"cube")
	_summon_item(Vector2i(56,-30),"cube")
	_summon_item(Vector2i(61,-21),"cube")
	_summon_item(Vector2i(65,-30),"triangle")
	_summon_item(Vector2i(50,-21),"triangle")
	_summon_fin(Vector2i(50,-25),["level6_fin","level6_pass"],4,Vector2i(48,-27))

func _level_7(reset=false):
	if reset:
		global.flag_list["level7_fin"]=0
		heart.pos=Vector2i(3,29)-offset
		layer_1.set_cell(Vector2i(-9,17),3,Vector2i(0,0))
	_summon_shard(Vector2i(-5,24),"G")
	_summon_shard(Vector2i(-2,21),"G")
	_summon_shard(Vector2i(-2,27),"G")
	_summon_shard(Vector2i(1,24),"G")
	_summon_item(Vector2i(1,28),"diamond")
	_summon_item(Vector2i(-1,26),"diamond")
	_summon_item(Vector2i(-2,23),"diamond")
	_summon_item(Vector2i(-2,24),"diamond")
	_summon_item(Vector2i(2,27),"diamond")
	_summon_item(Vector2i(1,29),"cube")
	_summon_item(Vector2i(-1,25),"cube")
	_summon_item(Vector2i(-3,24),"cube")
	_summon_item(Vector2i(0,25),"cube")
	_summon_item(Vector2i(3,27),"cube")
	_summon_item(Vector2i(2,28),"cube")
	_summon_item(Vector2i(3,28),"triangle")
	_summon_item(Vector2i(2,29),"triangle")
	_summon_item(Vector2i(-2,25),"triangle")
	_summon_item(Vector2i(-1,24),"triangle")
	_summon_fin(Vector2i(-7,19),["level7_fin","level7_pass"],4,Vector2i(-9,17))
	
func _level_8(reset=false):
	if reset:
		global.flag_list["level8_fin"]=0
		heart.pos=Vector2i(44,-3)-offset
		layer_1.set_cell(Vector2i(44,-5),3,Vector2i(0,0))
	_summon_shard(Vector2i(39,-8),"H")
	_summon_shard(Vector2i(47,-4),"H")
	_summon_item(Vector2i(44,-1),"diamond")
	_summon_item(Vector2i(44,0),"diamond")
	_summon_item(Vector2i(44,1),"diamond")
	_summon_item(Vector2i(44,2),"diamond")
	_summon_item(Vector2i(44,3),"cube")
	_summon_item(Vector2i(44,4),"cube")
	_summon_item(Vector2i(44,5),"cube")
	_summon_item(Vector2i(44,6),"cube")
	_summon_fin(Vector2i(44,-3),["level8_fin","level8_pass"],2,Vector2i(44,-5))	
	
func _level_9(reset=false):
	if reset:
		global.flag_list["level9_fin"]=0
		heart.pos=Vector2i(20,-10)-offset
		layer_1.set_cell(Vector2i(18,-12),3,Vector2i(0,0))
	_summon_shard(Vector2i(20,-4),"I")
	_summon_shard(Vector2i(26,-10),"I")
	_summon_shard(Vector2i(26,-4),"I")
	_summon_item(Vector2i(21,-6),"diamond")
	_summon_item(Vector2i(25,-7),"diamond")
	_summon_item(Vector2i(22,-10),"diamond")
	_summon_item(Vector2i(20,-3),"diamond")
	_summon_item(Vector2i(23,-5),"diamond")
	_summon_item(Vector2i(23,-7),"diamond")
	_summon_item(Vector2i(26,-3),"diamond")
	_summon_item(Vector2i(27,-4),"cube")
	_summon_item(Vector2i(21,-7),"cube")
	_summon_item(Vector2i(24,-7),"cube")
	_summon_item(Vector2i(22,-8),"cube")
	_summon_item(Vector2i(20,-9),"cube")
	_summon_item(Vector2i(26,-11),"cube")
	_summon_item(Vector2i(21,-10),"triangle")
	_summon_item(Vector2i(19,-4),"triangle")
	_summon_item(Vector2i(25,-9),"triangle")
	_summon_item(Vector2i(23,-8),"triangle")
	_summon_item(Vector2i(23,-6),"triangle")
	_summon_item(Vector2i(27,-10),"triangle")
	_summon_fin(Vector2i(20,-10),["level9_fin","level9_pass"],3,Vector2i(18,-12))
		
func _reset():
	heart.unstable=false
	var item_list=items.get_children()
	for i in item_list:
		i.queue_free()
	var shard_list=shards.get_children()
	for i in shard_list:
		i.queue_free()
	var fin=finale.get_children()
	for i in fin:
		i.queue_free()
	var path_list=layer_1.get_used_cells()
	for i in path_list:
		if layer_1.get_cell_source_id(i) in [0,2]:
			layer_1.erase_cell(i)
			layer_0.set_cell(i,0,Vector2i(0,7))
	global.cube=0
	global.triangle=0
	global.diamond=0
	if not global.cur_checkpoint:
		_level_1(true)
	elif global.flag_list["level9_enter"]:
		_level_9(true)
	elif global.flag_list["level8_enter"]:
		_level_8(true)
	elif global.flag_list["level7_enter"]:
		_level_7(true)
	elif global.flag_list["level6_enter"]:
		_level_6(true)
	elif global.flag_list["level5_enter"]:
		_level_5(true)
	elif global.flag_list["level4_enter"]:
		_level_4(true)
	elif global.flag_list["level3_enter"]:
		_level_3(true)
	elif global.flag_list["level2_enter"]:
		_level_2(true)

func _ready() -> void:
	_reset()
